package com.burgeon.backend.SHOPBACKEND.rest.openCardTask;

import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;


public class FindSTOREODMARKByModTask extends RESTBase {


    private String ip;
    private String account;
    private String password;

    public FindSTOREODMARKByModTask(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }


    /**
     * 发送并接收字符创
     *
     * @return
     */

    public ArrayList<HashMap<String,String>> getData(String arr[],String time) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";


        System.err.println(Arrays.toString(arr));
        String str = RESULT(Data(arr,time), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(str);
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<HashMap<String,String>> data = new ArrayList<>();

        for (int i=0;i<jsonArray.length();i++){
            HashMap<String,String> bean = new HashMap<String,String>();
            bean.put("sid",jsonArray.getJSONArray(i).getString(1));//门店id
            bean.put("mid",jsonArray.getJSONArray(i).getString(0));//任务id
            bean.put("exist","Y");//已存在 修改
            data.add(bean);
        }
        for (String id : arr){

            boolean b = true;
            for (HashMap<String,String> map:data){
                String sid = map.get("sid");

                if (id.equals(sid)){ //存在
                    b = false;
                }
            }
            if (b){
                HashMap<String,String> bean = new HashMap<String,String>();
                bean.put("sid",id);
                bean.put("exist","N");
                data.add(bean);
            }

        }

        return data;
    }

    private MultiValueMap Data(String[] arr, String time) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");

        columns.add("C_STORE_ID");



        params.put("columns", columns);
        params.put("table", "C_STOREODMARK");//表名


//        HashMap paramss = new HashMap();
//        paramss.put("column", "C_STORE_ID");
//        paramss.put("condition", "\" IN ("+ toString(arr)+")\"");
//        params.put("params", paramss);
        //**********************************
        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();

        expr1.put("column", "'C_STORE_ID'");
        expr1.put("condition",  "\" IN ("+ toString(arr)+")\"");

        HashMap expr2 = new HashMap();

        expr2.put("column","YEARMONTH");
        expr2.put("condition","'"+time+"'");

        paramss.put("combine", "and");
        paramss.put("expr1",expr1);
        paramss.put("expr2",expr2);

        params.put("params", paramss);


        return DATA("Query", params, account, password);
    }

    private String toString(String[] data) {

        return Arrays.toString(data).replace("\"", "'")
                .replace("[", "")
                .replace("]", "")
                .replace(" ", "");

    }

    public static void main(String[] args) {
        FindSTOREODMARKByModTask querryYCRetailout = new FindSTOREODMARKByModTask("39.104.73.78:5321", "nea@burgeon.com.cn", "burgeon5321");
        try {
            String[] arr = new String[]{"1836","1837","113"};

            System.err.println(querryYCRetailout.getData(arr,"202102"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
